package org.example;

public class Test1 {
    //leetcode 450 删除二叉搜索树中的节点 https://leetcode.cn/problems/delete-node-in-a-bst/description/

    /**
     * f方法一：递归
     * @param root
     * @param key
     * @return
     */
    public TreeNode deleteNode1(TreeNode root, int key) {
        if (root == null) return null;
        if (root.val == key) {
            if (root.left == null) return root.right;
            else if (root.right == null) return root.left;
            else {
                TreeNode cur = root.right;
                while (cur.left != null) {
                    cur = cur.left;
                }
                cur.left = root.left;
                root = root.right;
                return root;
            }
        }
        if (root.val > key) root.left = deleteNode1(root.left, key);
        if (root.val < key) root.right = deleteNode1(root.right, key);
        return root;
    }
    public TreeNode deleteNode(TreeNode root, int key) {
        if (root == null) return null;
        TreeNode prev = null, cur = root;
        while (cur.val != key) {
            prev = cur;
            if (key > cur.val) cur = cur.right;
            else if (key < cur.val) {
                cur = cur.left;
            }else break;
        }
        if (cur.left == null && cur.right == null) {
            if (prev.left == cur) prev.left = null;
            else prev.right = null;
        }
        if (cur.right != null) {
            TreeNode tmp1 = cur.right, tmp2 = tmp1;
            while (tmp1.left != null) {
                tmp2 = tmp1;
                tmp1 = tmp1.left;
            }
            if (tmp1.right != null) {
                tmp2.left = tmp1.right;
            }
            if (prev.left == cur) {
                prev.left = tmp1;
            }else {
                prev.right = tmp1;
            }
            tmp1.left = cur.left;
            if (cur.right == tmp1) tmp1.right = null;
            else tmp1.right = cur.right;
        }
        else {
            TreeNode tmp1 = cur.left, tmp2 = tmp1;
            while (tmp1.right != null) {
                tmp2 = tmp1;
                tmp1 = tmp1.right;
            }
            if (tmp1.left != null) {
                tmp2.right = tmp1.left;
            }
            if (prev.left == cur) {
                prev.left = tmp1;
            }else {
                prev.right = tmp1;
            }
            tmp1.right = cur.right;
            if (cur.left == tmp1) tmp1.left = null;
            else tmp1.left = cur.left;
        }
        return root;
    }

    public static void main(String[] args) {
        TreeNode a = new TreeNode(2);
        TreeNode b = new TreeNode(3);
        TreeNode c = new TreeNode(4);
        TreeNode d = new TreeNode(5);
        TreeNode e = new TreeNode(6);
        TreeNode f = new TreeNode(7);
        d.left = b;
        d.right = e;
        b.left = a;
        b.right = c;
        e.right = f;
        Test1 test1 = new Test1();
        test1.deleteNode(d,3);
        System.out.println(444);
    }
}
